package com.hongdee.poi;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class ExcelReader {
    private static final Log LOG = LogFactory.getLog(ExcelReader.class);
    private static final int READ_ALL = -1;

    private InputStream inputStream;
    private Workbook workbook;
    private SheetContentsHandler sheetContentsHandler;
    private ExcelSheetCallback sheetCallback;
    String[] columnIndex;


    public ExcelReader(InputStream inputStream, XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler, ExcelSheetCallback sheetCallback)
            throws Exception {
        this.columnIndex =
                new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        this.inputStream = inputStream;
        this.sheetContentsHandler = sheetContentsHandler;
        this.sheetCallback = sheetCallback;
    }

//    public ExcelReader(InputStream inputStream, XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler, ExcelSheetCallback sheetCallback) {
//        this.columnIndex =
//                new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
//        this.inputStream = inputStream;
//        this.sheetContentsHandler = sheetContentsHandler;
//        this.sheetCallback = sheetCallback;
//    }

    public ExcelReader() {

    }

    public void process()
            throws Exception {
        read(-1);
    }

    public void process(int sheetNumber)
            throws Exception {
        read(sheetNumber);
    }

    private void read(int sheetNumber) throws Exception {
        try {
            this.workbook = WorkbookFactory.create(this.inputStream);
            int sheetNum = this.workbook.getNumberOfSheets();

            Sheet sheet = null;
            for (int sheetIndex = 0; sheetIndex < sheetNum; ++sheetIndex) {
                sheet = this.workbook.getSheetAt(sheetIndex);
                if (this.sheetCallback != null) {
                    this.sheetCallback.startSheet(sheetIndex, sheet.getSheetName());
                }
                if ((-1 == sheetNumber) || (sheetIndex == sheetNumber)) {
                    readSheet(sheet);
                }

                if (this.sheetCallback != null)
                    this.sheetCallback.endSheet();
            }
        } catch (IOException ioe) {
            LOG.error(ioe.getMessage(), ioe.getCause());
        } catch (OpenXML4JException oxe) {
            LOG.error(oxe.getMessage(), oxe.getCause());
        }
    }

    private void readSheet(Sheet sheet) {
        Row row = null;
        Cell cell = null;
        int j = 0;
        if (sheet.getLastRowNum() == 0) {
            return;
        }
        for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); ++i) {
            row = sheet.getRow(i);

            this.sheetContentsHandler.startRow(i);
            for (j = row.getFirstCellNum(); j < row.getLastCellNum(); ++j) {
                cell = row.getCell(j);
                if (cell == null)
                    continue;
                this.sheetContentsHandler.cell(getColumnTitle(cell.getColumnIndex()) + i, getCellValue(cell));
            }

            this.sheetContentsHandler.endRow();
        }
    }

    private String getColumnTitle(int index) {
        String title = this.columnIndex[(index % 26)];
        while (index / 26 > 0) {
            index /= 26;
            title = title + this.columnIndex[index];
        }

        if (title.length() > 1) {
            title = title.substring(1) + title.substring(0, 1);
        }
        return title;
    }

    private String getCellValue(Cell cell) {
        String value = "";
        switch (cell.getCellType()) {
            case 3:
                break;
            case 4:
                value = new Boolean(cell.getBooleanCellValue()).toString();
                break;
            case 5:
                value = "" + cell.getErrorCellValue();
                break;
            case 2:
                value = cell.getCellFormula();
                break;
            case 0:
                if (HSSFDateUtil.isCellDateFormatted(cell) && null != cell.getDateCellValue()) {
                    Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(cell.getNumericCellValue());
                    value = String.valueOf(date.getTime());
                    break;
                }

                value = "" + cell.getNumericCellValue();
                break;
            case 1:
                value = cell.getStringCellValue();
        }

        return value;
    }

//    private static File getFile(String filePath) throws Exception {
//        if ((filePath == null) || (filePath.isEmpty())) {
//            throw new Exception("File path cannot be null");
//        }
//
//        return new File(filePath);
//    }
}

/* Location:           D:\Documents\Downloads\classes\classes\
 * Qualified Name:     com.myjeeva.poi.ExcelReader
 * JD-Core Version:    0.5.3
 */